<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="业务端已支付的订单id" prop="bizOrderNo">
        <el-input
          v-model="queryParams.bizOrderNo"
          placeholder="请输入业务端已支付的订单id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务端要退款的订单id，也就是子订单id" prop="bizRefundOrderNo">
        <el-input
          v-model="queryParams.bizRefundOrderNo"
          placeholder="请输入业务端要退款的订单id，也就是子订单id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="付款时传入的支付单号" prop="payOrderNo">
        <el-input
          v-model="queryParams.payOrderNo"
          placeholder="请输入付款时传入的支付单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款单号，每次退款的唯一标示" prop="refundOrderNo">
        <el-input
          v-model="queryParams.refundOrderNo"
          placeholder="请输入退款单号，每次退款的唯一标示"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="本次退款金额，单位分" prop="refundAmount">
        <el-input
          v-model="queryParams.refundAmount"
          placeholder="请输入本次退款金额，单位分"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总金额，单位分" prop="totalAmount">
        <el-input
          v-model="queryParams.totalAmount"
          placeholder="请输入总金额，单位分"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否是拆单退款，默认false" prop="isSplit">
        <el-input
          v-model="queryParams.isSplit"
          placeholder="请输入是否是拆单退款，默认false"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付渠道编码" prop="payChannelCode">
        <el-input
          v-model="queryParams.payChannelCode"
          placeholder="请输入支付渠道编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="第三方交易编码" prop="resultCode">
        <el-input
          v-model="queryParams.resultCode"
          placeholder="请输入第三方交易编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="第三方交易信息" prop="resultMsg">
        <el-input
          v-model="queryParams.resultMsg"
          placeholder="请输入第三方交易信息"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款渠道" prop="refundChannel">
        <el-input
          v-model="queryParams.refundChannel"
          placeholder="请输入退款渠道"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务端退款通知失败次数" prop="notifyFailedTimes">
        <el-input
          v-model="queryParams.notifyFailedTimes"
          placeholder="请输入业务端退款通知失败次数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据创建人，一般手动对账产生的单据才有值" prop="creater">
        <el-input
          v-model="queryParams.creater"
          placeholder="请输入单据创建人，一般手动对账产生的单据才有值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据修改人，一般手动对账产生的单据才有值" prop="updater">
        <el-input
          v-model="queryParams.updater"
          placeholder="请输入单据修改人，一般手动对账产生的单据才有值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="逻辑删除" prop="deleted">
        <el-input
          v-model="queryParams.deleted"
          placeholder="请输入逻辑删除"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['order:refundOrder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['order:refundOrder:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:refundOrder:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['order:refundOrder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="refundOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="业务端已支付的订单id" align="center" prop="bizOrderNo" />
      <el-table-column label="业务端要退款的订单id，也就是子订单id" align="center" prop="bizRefundOrderNo" />
      <el-table-column label="付款时传入的支付单号" align="center" prop="payOrderNo" />
      <el-table-column label="退款单号，每次退款的唯一标示" align="center" prop="refundOrderNo" />
      <el-table-column label="本次退款金额，单位分" align="center" prop="refundAmount" />
      <el-table-column label="总金额，单位分" align="center" prop="totalAmount" />
      <el-table-column label="是否是拆单退款，默认false" align="center" prop="isSplit" />
      <el-table-column label="支付渠道编码" align="center" prop="payChannelCode" />
      <el-table-column label="第三方交易编码" align="center" prop="resultCode" />
      <el-table-column label="第三方交易信息" align="center" prop="resultMsg" />
      <el-table-column label="退款状态，0：未提交，1：退款中，2：退款失败，3：退款成功" align="center" prop="status" />
      <el-table-column label="退款渠道" align="center" prop="refundChannel" />
      <el-table-column label="业务端退款通知失败次数" align="center" prop="notifyFailedTimes" />
      <el-table-column label="退款接口通知状态，0：待通知，1：通知成功，2：通知中，3：通知失败" align="center" prop="notifyStatus" />
      <el-table-column label="单据创建人，一般手动对账产生的单据才有值" align="center" prop="creater" />
      <el-table-column label="单据修改人，一般手动对账产生的单据才有值" align="center" prop="updater" />
      <el-table-column label="逻辑删除" align="center" prop="deleted" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['order:refundOrder:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['order:refundOrder:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改退款订单对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="refundOrderRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="业务端已支付的订单id" prop="bizOrderNo">
          <el-input v-model="form.bizOrderNo" placeholder="请输入业务端已支付的订单id" />
        </el-form-item>
        <el-form-item label="业务端要退款的订单id，也就是子订单id" prop="bizRefundOrderNo">
          <el-input v-model="form.bizRefundOrderNo" placeholder="请输入业务端要退款的订单id，也就是子订单id" />
        </el-form-item>
        <el-form-item label="付款时传入的支付单号" prop="payOrderNo">
          <el-input v-model="form.payOrderNo" placeholder="请输入付款时传入的支付单号" />
        </el-form-item>
        <el-form-item label="退款单号，每次退款的唯一标示" prop="refundOrderNo">
          <el-input v-model="form.refundOrderNo" placeholder="请输入退款单号，每次退款的唯一标示" />
        </el-form-item>
        <el-form-item label="本次退款金额，单位分" prop="refundAmount">
          <el-input v-model="form.refundAmount" placeholder="请输入本次退款金额，单位分" />
        </el-form-item>
        <el-form-item label="总金额，单位分" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入总金额，单位分" />
        </el-form-item>
        <el-form-item label="是否是拆单退款，默认false" prop="isSplit">
          <el-input v-model="form.isSplit" placeholder="请输入是否是拆单退款，默认false" />
        </el-form-item>
        <el-form-item label="支付渠道编码" prop="payChannelCode">
          <el-input v-model="form.payChannelCode" placeholder="请输入支付渠道编码" />
        </el-form-item>
        <el-form-item label="第三方交易编码" prop="resultCode">
          <el-input v-model="form.resultCode" placeholder="请输入第三方交易编码" />
        </el-form-item>
        <el-form-item label="第三方交易信息" prop="resultMsg">
          <el-input v-model="form.resultMsg" placeholder="请输入第三方交易信息" />
        </el-form-item>
        <el-form-item label="退款渠道" prop="refundChannel">
          <el-input v-model="form.refundChannel" placeholder="请输入退款渠道" />
        </el-form-item>
        <el-form-item label="业务端退款通知失败次数" prop="notifyFailedTimes">
          <el-input v-model="form.notifyFailedTimes" placeholder="请输入业务端退款通知失败次数" />
        </el-form-item>
        <el-form-item label="单据创建人，一般手动对账产生的单据才有值" prop="creater">
          <el-input v-model="form.creater" placeholder="请输入单据创建人，一般手动对账产生的单据才有值" />
        </el-form-item>
        <el-form-item label="单据修改人，一般手动对账产生的单据才有值" prop="updater">
          <el-input v-model="form.updater" placeholder="请输入单据修改人，一般手动对账产生的单据才有值" />
        </el-form-item>
        <el-form-item label="逻辑删除" prop="deleted">
          <el-input v-model="form.deleted" placeholder="请输入逻辑删除" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="RefundOrder">
import { listRefundOrder, getRefundOrder, delRefundOrder, addRefundOrder, updateRefundOrder } from "@/api/order/refundOrder";

const { proxy } = getCurrentInstance();

const refundOrderList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    bizOrderNo: null,
    bizRefundOrderNo: null,
    payOrderNo: null,
    refundOrderNo: null,
    refundAmount: null,
    totalAmount: null,
    isSplit: null,
    payChannelCode: null,
    resultCode: null,
    resultMsg: null,
    status: null,
    refundChannel: null,
    notifyFailedTimes: null,
    notifyStatus: null,
    creater: null,
    updater: null,
    deleted: null
  },
  rules: {
    bizOrderNo: [
      { required: true, message: "业务端已支付的订单id不能为空", trigger: "blur" }
    ],
    bizRefundOrderNo: [
      { required: true, message: "业务端要退款的订单id，也就是子订单id不能为空", trigger: "blur" }
    ],
    payOrderNo: [
      { required: true, message: "付款时传入的支付单号不能为空", trigger: "blur" }
    ],
    refundOrderNo: [
      { required: true, message: "退款单号，每次退款的唯一标示不能为空", trigger: "blur" }
    ],
    refundAmount: [
      { required: true, message: "本次退款金额，单位分不能为空", trigger: "blur" }
    ],
    totalAmount: [
      { required: true, message: "总金额，单位分不能为空", trigger: "blur" }
    ],
    isSplit: [
      { required: true, message: "是否是拆单退款，默认false不能为空", trigger: "blur" }
    ],
    payChannelCode: [
      { required: true, message: "支付渠道编码不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "退款状态，0：未提交，1：退款中，2：退款失败，3：退款成功不能为空", trigger: "change" }
    ],
    notifyFailedTimes: [
      { required: true, message: "业务端退款通知失败次数不能为空", trigger: "blur" }
    ],
    notifyStatus: [
      { required: true, message: "退款接口通知状态，0：待通知，1：通知成功，2：通知中，3：通知失败不能为空", trigger: "change" }
    ],
    createTime: [
      { required: true, message: "退款单据创建时间不能为空", trigger: "blur" }
    ],
    updateTime: [
      { required: true, message: "退款单据修改时间不能为空", trigger: "blur" }
    ],
    creater: [
      { required: true, message: "单据创建人，一般手动对账产生的单据才有值不能为空", trigger: "blur" }
    ],
    updater: [
      { required: true, message: "单据修改人，一般手动对账产生的单据才有值不能为空", trigger: "blur" }
    ],
    deleted: [
      { required: true, message: "逻辑删除不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询退款订单列表 */
function getList() {
  loading.value = true;
  listRefundOrder(queryParams.value).then(response => {
    refundOrderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    bizOrderNo: null,
    bizRefundOrderNo: null,
    payOrderNo: null,
    refundOrderNo: null,
    refundAmount: null,
    totalAmount: null,
    isSplit: null,
    payChannelCode: null,
    resultCode: null,
    resultMsg: null,
    status: null,
    refundChannel: null,
    notifyFailedTimes: null,
    notifyStatus: null,
    createTime: null,
    updateTime: null,
    creater: null,
    updater: null,
    deleted: null
  };
  proxy.resetForm("refundOrderRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加退款订单";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getRefundOrder(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改退款订单";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["refundOrderRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateRefundOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addRefundOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除退款订单编号为"' + _ids + '"的数据项？').then(function() {
    return delRefundOrder(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('order/refundOrder/export', {
    ...queryParams.value
  }, `refundOrder_${new Date().getTime()}.xlsx`)
}

getList();
</script>
